package com.lhkj.ct.admin.modules.recycle.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.meta.modules.recycle.entity.TblRecycleBin;
import com.lhkj.ct.meta.modules.recycle.service.RecycleBinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-10-09
 */
@RestController
@RequestMapping("api/plat/recycle/bin")
@Api(tags = {"8.1回收站"})
@Validated
public class TblRecycleBinController {

    @Resource
    private RecycleBinService recycleBinService;

    @GetMapping(value = "list")
    @ApiOperation(value = "1.列表查询接口")
    public ActionResult<IPage<TblRecycleBin>> list(TblRecycleBin entity, PageQuery pageQuery) {
        return ActionResult.success( recycleBinService.selectRecyclePage(entity, pageQuery));
    }

    @PutMapping("reverse")
    @ApiOperation(value = "2.恢复", notes = "支持批量恢复")
    public ActionResult<Void> reverse(@RequestBody @NotEmpty(message = "请选择需要恢复的记录") List<String> ids) {
        int count = 0;
        for (String id : ids) {
            count += recycleBinService.reverse(id);
        }
        if (count == 0) {
            return ActionResult.fail("操作失败");
        }
        return ActionResult.format("操作成功，共恢复[%d]条", count);
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "3.删除", notes = "支持批量删除")
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "请选择需要删除的记录") List<String> ids) {
        int count = recycleBinService.removeByIds(ids);
        if (count == 0) {
            return ActionResult.fail("操作失败");
        }
        return ActionResult.format("操作成功，共删除[%d]条", count);
    }
}
